// api.js
import store from '@/store'
const baseUrl = (process.env.NODE_ENV === "production") ?
				'https://www.faststormstu.site/api/' :
				'http://thinkvbenadmin.com/api/'
import { loginMpWx } from '@/api/login.js'
 
function request(options) {
  return new Promise((resolve, reject) => {
    const fullUrl = baseUrl + options.url;
	options.header = options.header ?? {}
	options.header['Content-Type'] = 'application/json'
	// 请求前加入当前终端
	options.header['platform'] = store.getters.platform
	// 请求前加入Token
	options.header['Authorization'] = store.getters.token
    uni.request({
      url: fullUrl,
      method: options.method || 'GET',
      data: options.data || {},
      header: options.header,
      success: (res) => {
        if (res.statusCode === 200) {
		  if(res.data.code == 500){
			  uni.showToast({
			  	title: res?.data?.message ?? 'Error',
			  	icon: "none"
			  });
			  reject(res.data);
		  }
          resolve(res.data.data);
        } else if(res.statusCode === 401){
			uni.hideLoading()
			uni.navigateTo({
				url: '/pages/my/login'
			})
		} else {
		  uni.showToast({
		  	title: res?.data?.message ?? 'Error',
		  	icon: "none"
		  });
          reject(res.data);
        }
      },
      fail: (err) => {
		uni.hideLoading()
        reject(err);
      }
    });
  });
}
 
// 导出API函数
export default {
  get(url, data, header) {
    return request({ url, method: 'GET', data, header });
  },
  post(url, data, header) {
    return request({ url, method: 'POST', data, header });
  },
  uploadImg : baseUrl+'upload/img',
  // 可以根据需要添加更多的HTTP方法
};